#include <QtCore/QCoreApplication>
#include <QScriptEngine>
#include <QFile>
#include <QScriptValue>

#include <QDebug>
#include "instanceholder.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    if (argc < 2) {
        qDebug() << "script file not defined";
        return 0;
    }

    qDebug() << "script file " << argv[1];

    QFile f(argv[1]);
    if (!f.open(QIODevice::ReadOnly)) {
        qDebug() << "error while opening file";
        return 0;
    };

    QString scr = QString::fromUtf8(f.readAll());
    f.close();

    QScriptEngine e;
    LibLoader* loader = LibLoader::getInstance();
    InstanceHolder ih(loader, &e);

    QScriptValue vLoader = e.newQObject(loader);
    QScriptValue vIh = e.newQObject(&ih);

    e.globalObject().setProperty("Loader", vLoader); // now you can access the LibLoader from a script by id Loader
    e.globalObject().setProperty("Holder", vIh);     // now you can access the InstanceHolder from a script by id Holder

    QScriptValue res = e.evaluate(scr);

    qDebug() << "result = " << res.toString();

    return a.exec();
}
